//
//  FiveContentCell.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/9.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class FiveContentCell: UITableViewCell {

    var titleLb:UILabel?;
    var timeLb:UILabel?;
    var contentLb:UILabel?;
    var dataDic:Dictionary<String,AnyObject>?;
    var isShowCell:Bool = false;
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        
        self.backgroundColor = .white;
        self.onCreateUIForView();
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func onCreateUIForView() {
        self.titleLb = UILabel(frame: CGRect(x: 20, y: 0, width: screenMainSize.width-40, height: 40));
        self.titleLb?.backgroundColor = .clear;
        self.titleLb?.textColor = .black;
        self.titleLb?.textAlignment = .left;
        self.titleLb?.font = UIFont.systemFont(ofSize: 18);
        self.contentView.addSubview(self.titleLb!);
        
        self.timeLb = UILabel(frame: CGRect(x: (self.titleLb?.frame.minX)!, y: (self.titleLb?.frame.maxY)!, width: (self.titleLb?.frame.width)!, height: 20));
        self.timeLb?.backgroundColor = .clear;
        self.timeLb?.textAlignment = .left;
        self.timeLb?.textColor = .lightGray;
        self.timeLb?.font = UIFont.systemFont(ofSize: 16);
        self.contentView.addSubview(self.timeLb!);
        
        self.contentLb = UILabel(frame: CGRect(x: (self.titleLb?.frame.minX)!, y: (self.timeLb?.frame.maxY)!, width: (self.titleLb?.frame.width)!, height: 40));
        self.contentLb?.backgroundColor = .clear;
        self.contentLb?.textAlignment = .left;
        self.contentLb?.textColor = .darkGray;
        self.contentLb?.font = UIFont.systemFont(ofSize: 14);
        self.contentLb?.numberOfLines = 2;
        self.contentView.addSubview(self.contentLb!);
    }
    
    public func onChangeDataForCell(dic:Dictionary<String,AnyObject>,isShow:Bool) {
        self.dataDic = dic;
        self.isShowCell = isShow;
        self.setNeedsLayout();
    }
    
    static func onShowHeight(isShow:Bool,dic:Dictionary<String,AnyObject>) -> CGFloat {
        var height = 0;
        
        if isShow {
            let contentName:String = (dic["content"] as! String);
            let contentRect = contentName.boundingRect(with:CGSize(width: screenMainSize.width-40, height: 1000), options: [NSStringDrawingOptions.usesLineFragmentOrigin,NSStringDrawingOptions.usesFontLeading], attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14)], context: nil);
            var itemHeight:CGFloat = contentRect.size.height+8;
            if itemHeight < 40 {
                itemHeight = 40;
            }
            height = Int(40 + 20 + itemHeight);
        } else {
            height = 40 + 20 + 40;
        }
        
        return CGFloat(height);
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if self.dataDic != nil {
            
            self.titleLb?.text = (self.dataDic!["title"] as! String);
            self.timeLb?.text = (self.dataDic!["time"] as! String);
            self.contentLb?.text = (self.dataDic!["content"] as! String);
            
            var contentHeight = 40;
            if self.isShowCell {
                self.contentLb?.numberOfLines = 0;
                
                let contentRect = self.contentLb?.text!.boundingRect(with:CGSize(width: screenMainSize.width-40, height: 1000), options: [NSStringDrawingOptions.usesLineFragmentOrigin,NSStringDrawingOptions.usesFontLeading], attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14)], context: nil);
                var itemHeight:CGFloat = contentRect!.size.height+8;
                if itemHeight < 40 {
                    itemHeight = 40;
                }
                contentHeight = Int(itemHeight);
            } else {
                self.contentLb?.numberOfLines = 2;
            }
            
            self.contentLb?.frame = CGRect(origin: (self.contentLb?.frame.origin)!, size: CGSize(width: Int(screenMainSize.width-40), height: contentHeight));
        }
    }
    
}
